/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pb138.accountingRecords.web;

import cz.muni.fi.pb138.accountingRecords.Payment;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author tuhar
 */
public class PaymentForm {

    private String id;
    private String bankAcc;
    private String destinationAcc;
    private BigDecimal amount;
    private Long variableSymbol;
    private String transferDate;
    private String note;

    public static PaymentForm extractFromRequest(HttpServletRequest request){
        PaymentForm paymentForm = new PaymentForm();
        paymentForm.setBankAcc(request.getParameter("bankAcc"));
        paymentForm.setDestinationAcc(request.getParameter("destinationAcc"));
        String amountString = request.getParameter("amount");
        if (amountString == null || amountString.equals("")) {
            paymentForm.setAmount(new BigDecimal(-20));
        } else {
                try{
                    paymentForm.setAmount(new BigDecimal(amountString));
                }catch(Exception ex){
                    paymentForm.setAmount(new BigDecimal(-20));
                }
        }
        String varString = request.getParameter("variableSymbol");
        if (varString == null || varString.equals("")) {
            paymentForm.setVariableSymbol(null);
        } else {
            try{
                paymentForm.setVariableSymbol(Long.parseLong(varString));
            }catch(NumberFormatException ex){
                paymentForm.setVariableSymbol(null);
            }
        }
        paymentForm.setTransferDate(request.getParameter("transferDate"));
        paymentForm.setNote(request.getParameter("note"));
        return paymentForm;
    }

    public Payment validateAndToPayment(StringBuilder errors) {
        Payment payment = new Payment();
        if (getBankAcc() == null || getBankAcc().isEmpty()) {
            errors.append("Odosielateľ nie je vyplnený").append(". <br />");
            payment.setBankAcc(null);
        } else {
            if (!getBankAcc().matches("[1-9][0-9]{8}/[0-9]{4}")) {
                errors.append("Účet odosielateľa je v zlom tvare").append(". <br />");
            } else {
                payment.setBankAcc(getBankAcc());
            }
        }
        if (getDestinationAcc() == null || getDestinationAcc().isEmpty()) {
            errors.append("Príjemca nie je vyplnený").append(". <br />");
            payment.setDestinationAcc(null);
        } else {
            if (!getDestinationAcc().matches("[1-9][0-9]{8}/[0-9]{4}")) {
                errors.append("Účet príjemcu je v zlom tvare").append(". <br />");
                payment.setDestinationAcc(null);
            } else {
                payment.setDestinationAcc(getDestinationAcc());
            }
        }
        if (getAmount().compareTo(new BigDecimal(-20))==0) {
            errors.append("Čiastka nie je vyplnená").append(". <br />");
            amount=null;
            payment.setAmount(null);
        } else {
            if (getAmount().compareTo(new BigDecimal(0)) <= 0) {
                errors.append("Čiastka je záporná").append(". <br />");
                payment.setAmount(null);
            } else {
                payment.setAmount(getAmount());
            }
        }
        if (getVariableSymbol() == null) {
            payment.setVariableSymbol(null);
        } else {
            if (getVariableSymbol() < 0 || getVariableSymbol() > 999999999l) {
                errors.append("Variabilný symbol je neplatný").append(". <br />");
                payment.setVariableSymbol(null);
            } else {
                payment.setVariableSymbol(getVariableSymbol());
            }
        }
        if (getTransferDate() == null || getTransferDate().equals("")) {
            errors.append("Dátum nie je vyplnený").append(". <br />");
            payment.setTransferDate(null);
        } else {
            try {
                SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
                payment.setTransferDate(formatter.parse(getTransferDate()));
            } catch (ParseException e) {
                errors.append("Dátom je v zlom tvare (správne:dd/mm/yyyy)").append(". <br />");
                payment.setTransferDate(null);
            }
        }
        if (getNote() != null || !getNote().equals("")) {
            payment.setNote(getNote());
        }
        if (errors.length() > 0) {
            return null;
        } else {
            return payment;
        }
    }

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @return the bankAcc
     */
    public String getBankAcc() {
        return bankAcc;
    }

    /**
     * @param bankAcc the bankAcc to set
     */
    public void setBankAcc(String bankAcc) {
        this.bankAcc = bankAcc;
    }

    /**
     * @return the destinationAcc
     */
    public String getDestinationAcc() {
        return destinationAcc;
    }

    /**
     * @param destinationAcc the destinationAcc to set
     */
    public void setDestinationAcc(String destinationAcc) {
        this.destinationAcc = destinationAcc;
    }

    /**
     * @return the amount
     */
    public BigDecimal getAmount() {
        return amount;
    }

    /**
     * @param amount the amount to set
     */
    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }

    /**
     * @return the variableSymbol
     */
    public Long getVariableSymbol() {
        return variableSymbol;
    }

    /**
     * @param variableSymbol the variableSymbol to set
     */
    public void setVariableSymbol(Long variableSymbol) {
        this.variableSymbol = variableSymbol;
    }

    /**
     * @return the transferDate
     */
    public String getTransferDate() {
        return transferDate;
    }

    /**
     * @param transferDate the transferDate to set
     */
    public void setTransferDate(String transferDate) {
        this.transferDate = transferDate;
    }

    /**
     * @return the note
     */
    public String getNote() {
        return note;
    }

    /**
     * @param note the note to set
     */
    public void setNote(String note) {
        this.note = note;
    }
}
